package com.hongxun.modules.bss.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.entity.APCostEntity;
import com.hongxun.modules.bss.service.APCostService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 项目其他费用
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-08-03 13:46:03
 */
@RestController
@RequestMapping("/bss/apcost")
public class APCostController {
	@Autowired
	private APCostService aPCostService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("state")!=null&&query.get("state").toString().equals("isdelno")) {
			wheres += " and  p_c_datastate != '删除' ";
		}
		if(query.get("pCToll")!=null&&!query.get("pCToll").toString().equals(""))
		{
			wheres+=" and  p_c_toll = '" + query.get("pCToll").toString()+"' ";
		}
        query.put("filterSql",wheres+" ");
		List<APCostEntity> aPCostList = aPCostService.queryList(query);
		int total = aPCostService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(aPCostList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		APCostEntity aPCost = aPCostService.queryObject(guid);
		
		return R.ok().put("aPCost", aPCost);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody APCostEntity aPCost){
		aPCostService.save(aPCost);
		
		return R.ok();
	}

	public void saveHZ(String pGuid ){
		Map<String, Object> params = new HashMap<String, Object>() ;
		params.put("pGuid",pGuid);
		Query query = new Query(params);
		String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("state")!=null&&query.get("state").toString().equals("isdelno")) {
			wheres += " and  p_c_datastate != '删除' ";
		}
		if(query.get("pCToll")!=null&&!query.get("pCToll").toString().equals(""))
		{
			wheres+=" and  p_c_toll = '" + query.get("pCToll").toString()+"' ";
		}
		query.put("filterSql",wheres+" ");
		List<APCostEntity> aPCostList = aPCostService.queryList(query);
		for(APCostEntity aPCostEntity : aPCostList  )
		{
			if(aPCostEntity.getPCToll().equals("是"))
			{
				BigDecimal dd = BigDecimal.ZERO;
				String  mm =aPCostEntity.getPCNum()+".";

				for(APCostEntity aPCostEntity1 : aPCostList  )
				{
					if(aPCostEntity1.getPCNum().contains(mm)&&aPCostEntity1.getPCPrice()!=null)
					{

						dd=dd.add(aPCostEntity1.getPCPrice());
					}
				}
				if(dd!=BigDecimal.ZERO) {
					aPCostEntity.setPCPrice(dd);
					aPCostService.update(aPCostEntity);
				}
			}

		}
	
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody APCostEntity aPCost){
		aPCost.setPCDatastate("修改");
		aPCostService.update(aPCost);
		
		return R.ok();
	}
	@RequestMapping("/updateprice")
	public R updateprice(@RequestParam Map<String, Object> params){

		APCostEntity aPCostEntity =	aPCostService.queryObject(params.get("guid").toString());
		aPCostEntity.setPCPrice(new BigDecimal(params.get("pCPrice").toString()) );
		aPCostEntity.setPCDatastate("修改");
		aPCostService.update(aPCostEntity);
		saveHZ(aPCostEntity.getPGuid());
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		aPCostService.deleteBatch(guids);
		
		return R.ok();
	}
	
}
